Ext.define('Triends.view.TingModify', {
 	extend: 'Ext.form.Panel',
 	requires: 'Triends.view.MapOverlay3',
 	xtype: 'tingmodify',
 	id: 'tingmodify',
 	config: {
 		fullscreen: true,
 		listeners: {
 			deactivate: function(oldActiveItem, container, newActiveItem, eOpts) {
					oldActiveItem.destroy();
				}
 		},
 		items: [
 			{
	 			xtype: 'fieldset',
	 			title: 'Title',
	 			items: [
	 				{
	 					xtype: 'textfield',
	 					id: 'tingmdftitle',
	 					style: 'border:3px inset'
 					}]
 			},
 			{	
 				xtype: 'panel',
 				layout: 'hbox',
 				width: "100%",
 				items: [{
	 				xtype: 'fieldset',
	 				layout: 'hbox',
	 				title: 'Theme',
	 				width: '92%',
	 				items: [
	 					{
			 				 xtype: 'selectfield',
			 				 style: 'border:3px inset',
			 				 flex: 9,
			 				 inputCls: 'selectfield',
			 				 usePicker: false,
			 				 id: 'tingmdftheme',
			 				 options: [
			 				 	{text: ' ', value: ' '},
							 	{text: '식사', value: 1},
							 	{text: '술', value: 2},
							 	{text: '야경', value: 3},
							 	{text: '공연 관람', value: 4},
							 	{text: '쇼핑', value: 5},
							 	{text: '클러빙', value: 6},
							 	{text: '전체', value: 7}
			 				 ],
			 				 listeners:{
			 				 	change: function(selectbox,newValue,oldValue){
			 				 		var tingmdfthemevalue = Ext.getCmp('tingmdftheme').getValue();
			 				 		Ext.getCmp('mdfthemeimage').setSrc('resource/images/'+tingmdfthemevalue+'.png');	
			 				 	}
			 				 }
						}
					]
 				},
 				{
					xtype: 'spacer',
					width: '2%'
				},
				{
					xtype: 'panel',
					docked: 'right',
					width: 48,
					height: 48,
					style: 'margin-top:45px',
					items: [{
						xtype: 'image',
						id: 'mdfthemeimage',
						width: 48,
						height: 48
					}]
				}]
 			},
 			{ 			
	 			xtype: 'panel',
	 			layout: 'vbox',
	 			items: [
	 			{
	 				xtype: 'fieldset',
	 				title: 'Date',
	 				items:[
				 			{
								xtype: 'datetimepickerfield',
								label: 'Date',
								style: 'border:3px inset',
								id: 'tingmdfdate',
								picker: {
										hideOnMaskTap: true,
										yearFrom: 2013,
										yearTo: 2020,
										minuteInterval:30,
										ampm: true,
										slotOrder: ['year', 'month', 'day', "ampm", "hour", "minute"],
										useTitles: true
								}
			 				}	
	 				]
	 			},
 				{
 					xtype: 'spacer'
 				},
 				{
 					xtype: 'panel',
 					style: 'margin-top:15px',
 					layout: 'vbox',
 					items:[
 						{
 							xtype: 'panel',
							style: 'margin-left:15px;margin-bottom:5px',
 							html: '<b>Location</b>'
 						},
 						{
 							xtype: 'button',
		 					iconCls: 'locate',
		 					iconMask: true,
		 					style: {'left':'50%', 'margin-left':'-50%', 'border': '1px outset'},
		 					width: '100%',
		 					handler: function(btn){
		 						var mapOverlay3 = Ext.create("Triends.view.MapOverlay3")
		 						
								var lat2 = Ext.getCmp("tingmodify").getData().LAT;
					 			var lng2 = Ext.getCmp("tingmodify").getData().LNG;
								
					 			var latlng = new google.maps.LatLng(lat2, lng2);
								var map4 = Ext.create("Ext.Map",{
									id: 'map4',
									width:300,
									mapOptions: {
										center: latlng,
										zoom: 14,
										zoomControl: true,
										mapTypeId: google.maps.MapTypeId.ROADMAP,
										streetViewControl: false,
										mapTypeControl: false,
										disableDoubleClickZoom: true
									},
									listeners: {
										maprender: function(comp, map){
											var latlng = new google.maps.LatLng(lat2, lng2);
											var selectedLocation;
											
											var marker = new google.maps.Marker({
												map: map,
												position: latlng,
												icon: 'resource/images/mappin1.png'
											});
											google.maps.event.addListener(map, 'dblclick', function(event){ // map은 항상 map, click이 아니라 mousedown 이벤트다!
												selectedLocation = event.latLng;
												marker.setPosition(selectedLocation);
												lat = selectedLocation.lat();
												lng = selectedLocation.lng();
											});	
										}
									}
								})
		 						mapOverlay3.add(map4);  
		 						Ext.Viewport.add(mapOverlay3);	
 						}
 					},
 					{
 						xtype: 'panel',
 						id: 'tingmdflocation',
 						style: 'margin-left:15px;margin-top:20px'
 					}]
 				}]	
 			},
	 		{
	 			xtype: 'fieldset',
	 			title: 'Discription',
	 			style: 'margin-top:30px',
	 			items: [{
	 				xtype: 'textareafield',
	 				id: 'tingmdfcontent',
	 				maxRows: 7,
	 				style: 'border:3px inset',
	 				height: 200
	 			}]
	 		},
 			{
 				xtype: 'panel',
 				layout: 'hbox',
 				items: [{
 					xtype: 'button',
 					width: '100%',
 					style: {'margin-top':'20px','left':'50%', 'margin-left':'-50%', 'border': '1px outset'},
		 			text: 'Modify',
		 			handler: function(){
		 				console.log('1');
		 				var userstoredata = Ext.getStore('UserStore').getAt(0).data;
						var userid = userstoredata.ID;
		 				var tingnum = Ext.getCmp("tingmodify").getData().NUMBER;
						var tingmdftitle = Ext.getCmp("tingmdftitle").getValue();
						var tingmdftheme = Ext.getCmp("tingmdftheme").getValue();
						var tingmdfdate = Ext.getCmp("tingmdfdate").getValue();
 						var tingmdfcontent = Ext.getCmp("tingmdfcontent").getValue();
 						if(tingmdftitle == ""){
 							navigator.notification.confirm(
									'Title을 입력하세요',
									function(){},
									'알림',
									'확인'
							)
							return;
						}
						if(tingmdftheme == " "){
							navigator.notification.confirm(
									'Theme을 입력하세요',
									function(){},
									'알림',
									'확인'
							)
							return;
						}
						if(tingmdfcontent == ""){
							navigator.notification.confirm(
									'Description을 입력하세요',
									function(){},
									'알림',
									'확인'
							)
							return;
						}
						Ext.Viewport.setMasked({
							xtype: 'loadmask',
		    				indicator: true
						});
 						Ext.Ajax.request({
							url: 'http://itscreater.cafe24.com/Triends/TingModify.jsp',
							params: {
								num: tingnum, 
								tingtitle: tingmdftitle, 
								tingcontent: tingmdfcontent, 
								tingtheme:tingmdftheme, 
								tingdate: tingmdfdate
							},
							useDefaultXhrHeader: false,
//							callbackkey: 'callback',
							success: function(response){
								var tingStore = Ext.getStore('tingstore');
								tingStore.loadPage(1);
								var userlat = UserStore.getAt(0).data.UserLat;
						        var userlng = UserStore.getAt(0).data.UserLng;
						    	var usertheme = UserStore.getAt(0).data.UserTheme;
						    	
								Ext.data.JsonP.request({
				        			url: 'http://itscreater.cafe24.com/Triends/UserHostTing.jsp',
				        			params: {id: userid,
												userlat: userlat, 
												userlng: userlng, 
												usertheme: usertheme},
				        			callbackKey: 'callback',
				        			success: function(result) {
				            			for(var i=0;i<result.length;i++){
											var date = result[i].DATE.split(' ')[0];
											var hour = result[i].DATE.split(' ')[1].substr(0,2);
											var minute = result[i].DATE.split(' ')[1].substr(2);
											var ampm;
											if(hour >= 12){
												hour = hour-12;
												ampm = "PM";
											} else{
												ampm = "AM";
											}
											var finaldate = date+" "+ampm+" "+hour+""+minute;
											result[i].AMPMDATE = finaldate; 
										}
								userHOSTtingStore.setData(result);
									userHOSTtingStore.load({
										callback: function() {
											Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
    											url: 'mainpage'
											}));
											Ext.Viewport.setMasked(false);
											navigator.notification.confirm(
												'Ting이 수정되었습니다.',
												function(){
													
												},
												'알림',
												'OK'
											)
										}
									});
									
				        		}
							});
						} // 
 					});
		 			}
 				}]
 			}]
 		}
});